package com.roadmap.util.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

import com.roadmap.common.BaseUtility;
import com.roadmap.common.RdcLogger;

public class MailDispatcher {
	private final static Logger logger = RdcLogger
			.getLogger(MailDispatcher.class);
	private static Session _session = null;
	private static InternetAddress _dispatcher;
	// private final static String _userId =
	// BaseUtility.getProperty("mail.dispatcher.id");
	// private final static String _password =
	// BaseUtility.getProperty("mail.dispatcher.password");
	// private final static String _host =
	// BaseUtility.getProperty("mail.dispatcher.smtp.host");

	static {
		String host = BaseUtility.getProperty("mail.dispatcher.smtp.host");
		final String user = BaseUtility.getProperty("mail.dispatcher.id");
		final String password = BaseUtility
				.getProperty("mail.dispatcher.password");

		Properties props = System.getProperties();
		props.put("mail.smtp.host", host); // set the mail dispatcher address
		props.put("mail.smtp.auth", "true"); // 

		_session = Session.getInstance(props, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(user, password);
			}
		});

		try {
			_dispatcher = new InternetAddress(BaseUtility
					.getProperty("mail.dispatcher.address"));
		}
		catch (Exception e) {
			logger.error("Initializing mail failed", e);
		}
	}

	@Deprecated
	public static boolean send(MailBean mail) {
		if (mail == null) {
			logger.error("the entered mail is NULL.");
			return false;
		}

		if (logger.isDebugEnabled()) {
			logger.debug(mail.toString());
		}

		try {
			Message msg = new MimeMessage(_session);
			msg.setFrom(_dispatcher);
			InternetAddress[] address = { new InternetAddress(mail
					.getRecipient()) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(mail.getSubject());
			Multipart mp = new MimeMultipart();
			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setText(mail.getContent());
			mp.addBodyPart(mbpContent);
			msg.setContent(mp);
			msg.setSentDate(new Date());
			Transport.send(msg);

			if (logger.isDebugEnabled()) {
				logger.debug("Successfully send a mail to "
						+ mail.getRecipient());
			}

			return true;
		}
		catch (Exception e) {
			// e.printStackTrace();
			logger.error("Sending mail failed.", e);
		}

		return false;
	}

	/** send mails with fixed dispatcher */
	public static boolean send(String mailAddr, String subject, String content) {
		if (mailAddr == null || subject == null || content == null) {
			return false;
		}

		try {
			Message msg = new MimeMessage(_session);
			msg.setFrom(_dispatcher);
			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
					mailAddr));
			msg.setSubject(subject);
			Multipart mp = new MimeMultipart();
			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setText(content);
			mp.addBodyPart(mbpContent);
			msg.setContent(mp);
			msg.setSentDate(new Date());
			Transport.send(msg);

			return true;
		}
		catch (Exception e) {
			logger.error("Sending mail failed.", e);
		}

		return false;
	}

}
